package com.example.wac.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.List;

/**
 * @author wuancheng
 * @description
 * @date 2023-05-31 18:06:00
 */

public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -2865798622445981925L;

    public static final String RESULT_CODE_FAIL = "0";
    public static final String RESULT_CODE_SUCCESS = "1";

    /**
     * 结果编号
     */
    private String resultCode;

    /**
     * 错误编号
     */
    private String errorCode;

    /**
     * 错误描述
     */
    private String errorDesc;

    private T data;

    /** 生成一个成功响应 */
    public static <T> BaseResponse<T> success() {
        BaseResponse<T> resp = new BaseResponse<>();
        resp.setResultCode(BaseResponse.RESULT_CODE_SUCCESS);
        return resp;
    }

    /**
     * 生成一个列表分页查询结果的响应
     *
     * @param list      查询结果
     * @param current   第几页
     * @param size      每页多少行
     * @param totalRows 一共多少行
     * @return 响应结果
     */
    public static <T> BaseResponse<PageResult<T>> success(List<T> list, Number current, Number size, Number totalRows) {
        PageResult<T> page = new PageResult<>();
        page.setRecords(list);
        page.setCurrent(current.longValue());
        page.setSize(size.longValue());
        page.setTotal(totalRows.longValue());

        return success(page);
    }

    /**
     * 生成成功的带实体数据的响应
     *
     * @param result 要返回的数据实体
     * @return 响应结果
     */
    public static <T> BaseResponse<T> success(T result) {
        BaseResponse<T> resp = new BaseResponse<>();
        resp.setResultCode(BaseResponse.RESULT_CODE_SUCCESS);
        resp.setData(result);
        return resp;
    }

    /**
     * 生成一个失败的响应
     *
     * @param errorCode 错误码
     * @param message   错误描述
     * @return 响应实体
     */
    public static <T> BaseResponse<T> fail(String errorCode, String message) {
        BaseResponse<T> resp = new BaseResponse<>();
        resp.setResultCode(BaseResponse.RESULT_CODE_FAIL);
        resp.setErrorCode(errorCode);
        resp.setErrorDesc(message);
        return resp;
    }

    public BaseResponse() {
        super();
    }

    public BaseResponse(String resultCode, String errorCode, String errorDesc) {
        super();
        this.resultCode = resultCode;
        this.errorCode = errorCode;
        this.errorDesc = errorDesc;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorDesc() {
        return errorDesc;
    }

    public void setErrorDesc(String errorDesc) {
        this.errorDesc = errorDesc;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return RESULT_CODE_SUCCESS.equals(this.getResultCode());
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
